package com.crazymaker.springcloud.base.core;

import com.crazymaker.springcloud.common.constants.SessionConstants;
import com.crazymaker.springcloud.common.context.SessionHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.session.web.http.HttpSessionIdResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

public class CustomedSessionIdResolver implements HttpSessionIdResolver {
    @Override
    public List<String> resolveSessionIds(HttpServletRequest request) {
        String sid = SessionHolder.getSid();
        if (StringUtils.isBlank(sid))
            return Collections.EMPTY_LIST;
        return Collections.singletonList(sid);
    }

    @Override
    public void setSessionId(HttpServletRequest request, HttpServletResponse response, String sessionId) {
        // 不需要返回session id给前端
        response.setHeader(SessionConstants.USER_IDENTIFIER, "" );
    }

    @Override
    public void expireSession(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader(SessionConstants.USER_IDENTIFIER, "" );
    }
}
